package com.ashin;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.strategy.InlineShardingStrategyConfiguration;
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Random;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {
        // 配置真实数据源
        Map<String, DataSource> dataSourceMap = new HashMap<>();

        // 配置第一个数据源
        DruidDataSource druidDataSource1 = new DruidDataSource();
        druidDataSource1.setDriverClassName("com.mysql.cj.jdbc.Driver");
        druidDataSource1.setUrl("jdbc:mysql://localhost:3306/test?serverTimezone=UTC");
        druidDataSource1.setUsername("root");
        druidDataSource1.setPassword("tiger");
        dataSourceMap.put("ds1", druidDataSource1);

        // 配置第二个数据源
        DruidDataSource druidDataSource2 = new DruidDataSource();
        druidDataSource2.setDriverClassName("com.mysql.cj.jdbc.Driver");
        druidDataSource2.setUrl("jdbc:mysql://localhost:3306/test1?serverTimezone=UTC");
        druidDataSource2.setUsername("root");
        druidDataSource2.setPassword("tiger");
        dataSourceMap.put("ds2", druidDataSource2);

        // 配置Order表规则
        TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration("orders", "ds${1..2}.orders_${1..2}");

        // 配置分库 + 分表策略
        orderTableRuleConfig.setDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("customer_id", "ds${customer_id % 2 + 1}"));
        orderTableRuleConfig.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("id", "orders_${id % 2 + 1}"));

        // 配置分片规则
        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);

        // 省略配置order_item表规则...
        // ...

        // 获取数据源对象
        try {
            DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, new Properties());

            // 获取数据库连接
            Connection connection = dataSource.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement("insert into orders(id,customer_id,num,price) values(?,?,?,?)");
            int count = 10;
            for (int startId = 20, maxId = startId + count; startId < maxId; startId++) {
                System.out.println("插入的数据id:" + startId);
                preparedStatement.setLong(1, startId);
                preparedStatement.setLong(2, startId);
                preparedStatement.setInt(3, new Random().nextInt(10));
                preparedStatement.setDouble(4, startId * 100.0);
                preparedStatement.execute();
            }

            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
